package com.asset.service.impl;

import com.asset.mapper.BrandMapper;
import com.asset.mapper.CategoryMapper;
import com.asset.mapper.DepaMapper;
import com.asset.mapper.DepoMapper;
import com.asset.pojo.Brand;
import com.asset.pojo.Category;
import com.asset.pojo.Department;
import com.asset.pojo.Depository;
import com.asset.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

@Slf4j
@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DepaMapper depaMapper;

    @Resource
    private DepoMapper depoMapper;

    @Override
    public Boolean checkBrand(Long id) {
        log.debug("[common:check:brand] 检测 id 为 {} 的品牌信息是否存在", id);
        if (id != null) {
            Brand brand = new Brand();
            brand.setId(id);

            return 1 == brandMapper.selectCount(brand);
        }
        return false;
    }

    @Override
    public Boolean checkCategory(Long id, Boolean parent) {
        log.debug("[common:check:brand] 检测 id 为 {} 的分类信息是否存在，是否为上级分类：{}", id, parent);
        if (id != null) {
            Example example = new Example(Category.class);

            example.createCriteria().andEqualTo("id", id);

            if (parent) {
                example.createCriteria().andEqualTo("pid", 0);
            } else {
                example.createCriteria().andNotEqualTo("pid", 0);
            }

            return 1 == categoryMapper.selectCountByExample(example);
        }

        return false;
    }

    @Override
    public Boolean checkDepartment(Long id) {
        log.debug("[common:check:brand] 检测 id 为 {} 的部门信息是否存在", id);
        if (id != null) {
            Department record = new Department();
            record.setId(id);

            return 1 == depaMapper.selectCount(record);
        }

        return false;
    }

    @Override
    public Boolean checkDepository(Long id, Long depaId) {
        log.debug("[common:check:brand] 检测 id 为 {} 的部门信息是否存在，且其所属部门 id 为 {}", id, depaId);
        if (id != null) {
            Depository record = new Depository();
            record.setId(id);
            record.setDepaId(depaId);

            return 1 == depoMapper.selectCount(record);
        }

        return false;
    }
}
